package jhunter.rt.conf.ext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import jhunter.conf.services.vo.Service;
import jhunter.conf.services.vo.Services;
import jhunter.rt.conf.Node;
import jhunter.rt.conf.Reloadable;
import jhunter.service.ModuleService;
import jhunter.template.TemplateLoader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.ByteArrayResource;

public class ServicesExt implements Reloadable {

	private static Log log = LogFactory.getLog(ServicesExt.class);

	private Services services;

	private Node servicesNode;

	public ServicesExt(Services services, Node servicesNode) {
		super();
		this.services = services;
		this.servicesNode = servicesNode;
	}

	public Services getServices() {
		return services;
	}

	public void setServices(Services services) {
		this.services = services;
	}

	public Node getServicesNode() {
		return servicesNode;
	}

	public void setServicesNode(Node servicesNode) {
		this.servicesNode = servicesNode;
	}

	/**
	 * 加载GroovyService
	 * 
	 * @return
	 */
	public Map<String, ModuleService> loadGroovyService() {
		Map<String, ModuleService> serviceMap = new HashMap<String, ModuleService>();
		Map<String, Object> serviceIdMap = new LinkedHashMap<String, Object>();
		for (Service ser : this.services.getService()) {
			String id = ser.getId();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("methods", ser.getMethod());
			String className = "GroovyModuleService" + System.currentTimeMillis();
			params.put("className", className);
			params.put("daos", ser.getDaos() == null ? new ArrayList() : ser.getDaos().getDao());
			serviceIdMap.put(id, params);
		}
		Map<String, Object> value = new HashMap<String, Object>();
		value.put("services", serviceIdMap);
		
//		ModuleExt me = (ModuleExt) this.servicesNode.getParent().getObject();
//		value.put("daos", me.getEntityIds());
		String tp = TemplateLoader.getStringByTemplate("/jhunter/service/module-spring-context.xml", value);
		log.debug("\n" + tp);
		@SuppressWarnings("resource")
		GenericXmlApplicationContext module = new GenericXmlApplicationContext();
		module.setParent(((AppExt) this.servicesNode.getParent().getParent().getObject()).getAppSpringContext());
		module.load(new ByteArrayResource(tp.getBytes()));
		module.refresh();
		for (String key : serviceIdMap.keySet()) {
			serviceMap.put(key, (ModuleService) module.getBean(key));
		}
		return serviceMap;
	}

	protected Map<String, Object> getModuleService(Service service) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("methods", service.getMethod());
		return params;
	}

	@Override
	public void unload() {
		ModuleExt me = (ModuleExt) this.servicesNode.getParent().getObject();
		me.unloadService();
	}

	@Override
	public void afterLoad() {

	}

	@Override
	public void init() {
		afterLoad();
	}

}
